package com.d0318;

public class Station extends Thread{
//构造方法给线程名字赋值
    public Station(String name){
        super(name);//给线程名字赋值
    }
//为了保持票数一致
    static int tick = 20;
//创建一个静态钥匙
    static Object ob = "aa";

    @Override
    public void run() {
        while(tick>0){
            synchronized (ob){//使用一个锁，进去的人把钥匙拿在手上，出来才把钥匙拿出来
                if(tick>0){
                    System.out.println(getName()+"卖出了"+tick+"号票");
                    tick--;
                }else {
                    System.out.println("票卖完了");
                }
            }
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        }
    }

   class MainClass{
       public static void main(String[] args) {
           Station station1 = new Station("窗口1");
           Station station2 = new Station("窗口2");
           Station station3 = new Station("窗口3");

           station1.start();
           station2.start();
           station3.start();
       }
    }
